/*
* Copyright (C) 2021, KylinSoft Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/

#include "crop.h"

#include <ukui-log4qt.h>

CropLabel::CropLabel(QLabel *parent)
    : QLabel(parent)
{
    initCropSettings();
    loadAllPixmaps();

}

CropLabel::~CropLabel()
{

}

void CropLabel::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event); // draw label background

//    QPainter painter(this);
    painter.begin(this);
    QPen pen(QColor(55, 144, 250), 1);
    painter.setPen(pen);

    QPainterPath borderPath;
    borderPath.setFillRule(Qt::WindingFill);
    borderPath.addRect(0, 0, this->width(), this->height()); // need judge

//    int W = m_endX - m_startX;
//    int H = m_endY - m_startY;

    int W = m_endPoint.x() - m_beginPoint.x();
    int H = m_endPoint.y() - m_beginPoint.y();
    m_currentSelectRect = getRect(m_beginPoint, m_endPoint);

    QPainterPath cropPath;
    cropPath.addRect(m_currentSelectRect);


    // draw different pixmap
    drawPixmapBorder(m_currentSelectRect, W, H);


    // draw crop rect path
    painter.drawPath(cropPath);

    // draw the background of the area outside the clipping area
    QPainterPath outPath;
    outPath = borderPath.subtracted(cropPath);

    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.fillPath(outPath, QColor(0, 0, 0, 100));

    painter.end();

}

void CropLabel::mousePressEvent(QMouseEvent *event)
{
//    QApplication::setOverrideCursor(QCursor(Qt::ClosedHandCursor));

    if (event->button() == Qt::LeftButton) {
        m_beginX = event->pos().x();
        m_beginY = event->pos().y();
        m_endX = event->pos().x();
        m_endY = event->pos().y();

        m_beginPoint = QPoint(m_beginX, m_beginY);
        KyInfo() << "m_startPoint = " << m_beginPoint;

        m_isPressed = true;
    }
}

void CropLabel::mouseReleaseEvent(QMouseEvent *event)
{
    m_isPressed = false;

    if (event->buttons() & Qt::LeftButton) { // For avoid rightButton
        m_endX = event->pos().x();
        m_endY = event->pos().y();
    }

    m_endX = judgePosition(m_endX, 0, this->width());
    m_endY = judgePosition(m_endY, 0, this->height());

    m_endPoint = QPoint(m_endX, m_endY);

    KyInfo() << "m_endPoint = " << m_endPoint;
    update();

//    QApplication::restoreOverrideCursor();
}

void CropLabel::mouseMoveEvent(QMouseEvent *event)
{
    if (m_isPressed && (event->buttons() & Qt::LeftButton)) {
        m_endX = event->pos().x();
        m_endY = event->pos().y();

        m_endPoint = event->pos();
        m_movePoint = event->pos();
        KyInfo() << "m_movePooint = " << m_movePoint;

        update();
    }
}

int CropLabel::judgePosition(int origin, int min, int max)
{
    KyInfo() << "origin = " << origin
             << "min = " << min
             << "max = " << max;

    if (origin < min) {
        // left border
        return min;
    }
    if (origin > max) {
        // right border: In order to show green border, so need max-1
        return max-1;
    }

    return origin;
}

bool CropLabel::isPressPointInCropRegion(QPoint mousePressPoint)
{
    return true;
}

bool CropLabel::drawPixmapBorder(QRect rect, int W, int H)
{
    painter.drawPixmap(rect.x()-m_leftTopPixmapSize.width()/2, rect.y()-m_leftTopPixmapSize.height()/2,
                       m_leftTopPixmapSize.width(), m_leftTopPixmapSize.height(),  m_leftTopPixmap);

    painter.drawPixmap(rect.x()+W/2-m_topPixmapSize.width()/2, rect.y()-m_topPixmapSize.height()/2,
                       m_topPixmapSize.width(), m_topPixmapSize.height(), m_topPixmap);

    painter.drawPixmap(rect.x()+W-m_rightTopPixmapSize.width()/2, rect.y()-m_rightTopPixmapSize.height()/2,
                       m_rightTopPixmapSize.width(), m_rightTopPixmapSize.height(), m_rightTopPixmap);

    painter.drawPixmap(rect.x()+W-m_rightPixmapSize.width()/2, rect.y()+H/2-m_rightPixmapSize.height()/2,
                       m_rightPixmapSize.width(), m_rightPixmapSize.height(), m_rightPixmap);

    painter.drawPixmap(rect.x()+W-m_rightBottomPixmapSize.width()/2, rect.y()+H-m_rightBottomPixmapSize.height()/2,
                       m_rightBottomPixmapSize.width(), m_rightBottomPixmapSize.height(), m_rightBottomPixmap);

    painter.drawPixmap(rect.x()+W/2-m_bottomPixmapSize.width()/2, rect.y()+H-m_bottomPixmapSize.height()/2,
                       m_bottomPixmapSize.width(), m_bottomPixmapSize.height(), m_bottomPixmap);

    painter.drawPixmap(rect.x()-m_leftBottomPixmapSize.width()/2, rect.y()+H-m_leftBottomPixmapSize.height()/2,
                       m_leftBottomPixmapSize.width(), m_leftBottomPixmapSize.height(), m_leftBottomPixmap);

    painter.drawPixmap(rect.x()-m_leftPixmapSize.width()/2, rect.y()+H/2-m_leftPixmapSize.height()/2,
                       m_leftPixmapSize.width(), m_leftPixmapSize.height(), m_leftPixmap);

    return true;
}

QRect CropLabel::getRect(const QPoint &m_beginPoint, const QPoint &m_endPoint)
{
    int W = m_endPoint.x() - m_beginPoint.x();
    int H = m_endPoint.y() - m_beginPoint.y();

    QRect rect(m_beginPoint.x(), m_beginPoint.y(),  W, H);

    return rect;

}


void CropLabel::initCropSettings()
{
    // Not show crop box in first crop operation
    m_beginX = -10;
    m_beginY = -10;
    m_endX = -10;
    m_endY = -10;

    m_beginPoint = QPoint(-10, -10);
    m_endPoint = QPoint(-10, -10);

    m_hasCropRegion = false;
}

void CropLabel::loadAllPixmaps()
{
    m_leftTopPixmap.load(":/crop-left-top.svg");
    m_leftTopPixmapSize = QSize(10, 10);

    m_topPixmap.load(":/crop-top.svg");
    m_topPixmapSize = QSize(18, 10);

    m_rightTopPixmap.load(":/crop-right-top.svg");
    m_rightTopPixmapSize = QSize(10, 10);

    m_rightPixmap.load(":/crop-right.svg");
    m_rightPixmapSize = QSize(10, 18);

    m_rightBottomPixmap.load(":/crop-right-bottom.svg");
    m_rightBottomPixmapSize = QSize(10, 10);

    m_bottomPixmap.load(":/crop-bottom.svg");
    m_bottomPixmapSize = QSize(18, 10);

    m_leftBottomPixmap.load(":/crop-left-bottom.svg");
    m_leftBottomPixmapSize = QSize(10, 10);

    m_leftPixmap.load(":/crop-left.svg");
    m_leftPixmapSize = QSize(10, 18);
}
